package org.spring4speedtracer.thread;

import org.spring4speedtracer.collect.ServerTrace;
import org.spring4speedtracer.collect.TraceManager;
import org.spring4speedtracer.collect.TraceCollector.StepType;
import org.spring4speedtracer.perform.TracePerformer;

public class ServerTraceThreadBridge implements TraceThreadBridge {

	private TraceManager traceManager;

	private TracePerformer tracePerformer;

	private ServerTrace master;

	public ServerTraceThreadBridge(TraceManager traceManager,
			TracePerformer tracePerformer) {
		this.traceManager = traceManager;
		this.tracePerformer = tracePerformer;
		master = traceManager.getCurrentTrace();
		if (master != null) {
			master.anchor();
		}
	}

	@Override
	public void addThread(String name) {
		if (master != null) {
			master.setThreadName(name);
			traceManager.setCurrentTrace(master);
			tracePerformer.setEnabled(true);
			traceManager.beginStep("Thread-" + name, StepType.THREAD);
		}
	}

	@Override
	public void removeThread() {
		if (master != null) {
			traceManager.completeStep();
			traceManager.clearCurrentTrace();
			tracePerformer.setEnabled(false);
		}
	}
}
